package com.example.panpan.userManage.service;


import com.example.common.utils.IMap;
import com.example.panpan.userManage.mapper.TcUserMapper;
import com.example.panpan.userManage.model.TcUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class UserService {
    @Autowired
    private TcUserMapper tcUserMapper;

    /**
     * 修改密码
     *
     * @return
     */
    public int updateUserPwd(String userId,String oldPassword,String pwd){
        return tcUserMapper.updateUserPwd(userId, oldPassword, pwd);
    }

    /**
     * 获取用户所在部门
     * @return
     */
    public IMap getUserDp(String userId){
        return tcUserMapper.getUserDp(userId);
    }

    public int uptUser(IMap param){
        String userId = param.getString("userId");
        TcUser tcUser = tcUserMapper.selectByPrimaryKey(userId);
        tcUser.setUserName(param.getString("userName"));
        tcUser.setSex(param.getString("sex"));
        tcUser.setUserPhone(param.getString("telephone",""));
        tcUser.setDepartId(param.getString("departId"));
        if(param.getString("departId").equals("d001")){
            tcUser.setRoleId("02");
        }else{
            tcUser.setRoleId("03");
        }
        try {
            tcUserMapper.updateByPrimaryKeySelective(tcUser);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
    public int doUserIn(IMap param){
        TcUser tcUser = new TcUser();
        tcUser.setUserId(param.getString("userId"));
        tcUser.setState("1");
        try {
            tcUserMapper.updateByPrimaryKeySelective(tcUser);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
    public int alreadyLeave(IMap param){
        TcUser tcUser = new TcUser();
        tcUser.setUserId(param.getString("userId"));
        tcUser.setState("0");
        try {
            tcUserMapper.updateByPrimaryKeySelective(tcUser);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }

    public List getUserList(IMap params){
        List res = tcUserMapper.getUserList(params);
        return res;
    }

    public int addUser(IMap param){
        TcUser tcUser = new TcUser();
        tcUser.setUserId(param.getString("userId"));
        tcUser.setUserName(param.getString("userName"));
        tcUser.setPassword("123456");
        tcUser.setSex(param.getString("sex"));
        tcUser.setUserPhone(param.getString("telephone",""));
        tcUser.setDepartId(param.getString("departId"));
        String roleId = "";
        if(param.getString("departId").equals("d001")){
            roleId = "02";
        }else{
            roleId = "03";
        }
        tcUser.setRoleId(roleId);
        tcUser.setState("2");

        int k = tcUserMapper.insert(tcUser);

        return k;
    }

    public TcUser getUserInfo(String userId){
        TcUser tcUser = new TcUser();
        tcUser.setUserId(userId);
        tcUser = tcUserMapper.selectByPrimaryKey(userId);
        return tcUser;
    }
}
